﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using DualityClient.MenuComponents.Buttons;
using DualityClient.MenuComponents;
using DualityEngine.Business;

namespace DualityClient.Screens
{
    public class LoginScreen : Menu
    {
        MouseState currentmousestate;

        public Textbox usernametextbox { get; private set; }
        public Textbox passwordtextbox { get; private set; }
        private RegisterButton registerbutton;
        private LoginButton loginbutton;

        public LoginScreen()
            : base()
        {
            loginbutton = new LoginButton(ResourceManager.Instance.loginbutton, ResourceManager.Instance.loginbuttonhover, new Vector2(422.5f, 463.5f));
            registerbutton = new RegisterButton(ResourceManager.Instance.registerbutton, ResourceManager.Instance.registerbuttonhover, new Vector2(618f, 463.5f));
            usernametextbox = new Textbox(TEXTBOXTYPE.NONE, ResourceManager.Instance.logintextbox, ResourceManager.Instance.logintextboxhover, new Vector2(435f, 344f));
            passwordtextbox = new Textbox(TEXTBOXTYPE.PASSWORD, ResourceManager.Instance.logintextbox, ResourceManager.Instance.logintextboxhover, new Vector2(435f, 418f));
        }

        public override void Update(GameTime gametime)
        {
            base.Update(gametime);
            MouseState oldmousestate = currentmousestate;
            currentmousestate = Mouse.GetState();

            if(currentmousestate.LeftButton == ButtonState.Released && oldmousestate.LeftButton == ButtonState.Pressed) 
            {
                UndoTextBoxSelection();
            }

            usernametextbox.Update(gametime);
            passwordtextbox.Update(gametime);
            registerbutton.Update(gametime);
            loginbutton.Update(gametime);      
        }

        public override void Draw(GameTime gametime)
        {
            base.Draw(gametime);
            usernametextbox.Draw(gametime);
            passwordtextbox.Draw(gametime);
            registerbutton.Draw(gametime);
            loginbutton.Draw(gametime);
            ResourceManager.Instance.spriteBatch.Draw(ResourceManager.Instance.loginmenubackground, new Vector2(((ResourceManager.Instance.graphicsdevice.PresentationParameters.BackBufferWidth - ResourceManager.Instance.loginmenubackground.Width) / 2), ResourceManager.Instance.graphicsdevice.PresentationParameters.BackBufferHeight / 4), Color.White);
        }

        public void UndoTextBoxSelection()
        {
            if (usernametextbox.isselected == true)
            {
                usernametextbox.isselected = false;
            }
            else if (passwordtextbox.isselected == true)
            {
                passwordtextbox.isselected = false;
            }
        }
    }
}
